//package com.zryc.shop.rabbitmq;
//
//import com.zryc.shop.feign.IFeignProduct;
//import com.zryc.shop.pojo.Order;
//import com.zryc.shop.pojo.Product;
//import com.zryc.shop.service.IOrderService;
//import com.zryc.shop.util.JsonUtil;
//import io.seata.spring.annotation.GlobalTransactional;
//import org.springframework.amqp.core.AcknowledgeMode;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//@Service
//public class MQReceiver {
//
//    @Autowired
//    private IOrderService orderService;
//
//    @Autowired
//    private IFeignProduct feignProduct;
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @RabbitListener(queues="orderQueue")
//    //分布式事务，不能使用本地事务，需要使用seata全局事务
//    @GlobalTransactional
//    //@Transactional
//    public void receive(String message){
//        rabbitTemplate.containerAckMode(AcknowledgeMode.AUTO);
//        Order order = JsonUtil.jsonStr2Object(message, Order.class);
//        Product product = feignProduct.detail(order.getPid());
//        /*if(product.getStock()<1){
//            return;
//        }
//        Order order1  = (Order) redisTemplate.opsForValue().get("order:" + order.getUid() + ":" + order.getPid());
//        if(order1!=null){
//            return;
//        }*/
//        feignProduct.subStock(order.getPid(),order.getNumber());
//        orderService.save(order);
//        redisTemplate.opsForValue().set("order:"+order.getUid()+":"+order.getPid(),order);
//    }
//
//}
